<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="立杆编码" prop="poleCode">
        <el-input
          v-model="queryParams.poleCode"
          placeholder="请输入立杆编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="立杆名称" prop="poleName">
        <el-input
          v-model="queryParams.poleName"
          placeholder="请输入立杆名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="位置描述" prop="locationName">
        <el-input
          v-model="queryParams.locationName"
          placeholder="请输入位置描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input
          v-model="queryParams.longitude"
          placeholder="请输入经度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input
          v-model="queryParams.latitude"
          placeholder="请输入纬度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="摄像头IP" prop="cameraIp">
        <el-input
          v-model="queryParams.cameraIp"
          placeholder="请输入摄像头IP"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="RTSP端口(默认554)" prop="cameraPort">
        <el-input
          v-model="queryParams.cameraPort"
          placeholder="请输入RTSP端口(默认554)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="摄像头账号" prop="cameraUsername">
        <el-input
          v-model="queryParams.cameraUsername"
          placeholder="请输入摄像头账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="摄像头密码" prop="cameraPassword">
        <el-input
          v-model="queryParams.cameraPassword"
          placeholder="请输入摄像头密码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="厂商" prop="vendor">
        <el-input
          v-model="queryParams.vendor"
          placeholder="请输入厂商"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="摄像头型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入摄像头型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="启动时是否自动拉流" prop="autoStart">
        <el-input
          v-model="queryParams.autoStart"
          placeholder="请输入启动时是否自动拉流"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="httpPort">
        <el-input
          v-model="queryParams.httpPort"
          placeholder="请输入${comment}"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公里数" prop="km">
        <el-input
          v-model="queryParams.km"
          placeholder="请输入公里数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上一个杆子" prop="lastPole">
        <el-input
          v-model="queryParams.lastPole"
          placeholder="请输入上一个杆子"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['iot:poles:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['iot:poles:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['iot:poles:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['iot:poles:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="polesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="立杆ID" align="center" prop="id" />
      <el-table-column label="立杆编码" align="center" prop="poleCode" />
      <el-table-column label="立杆名称" align="center" prop="poleName" />
      <el-table-column label="位置描述" align="center" prop="locationName" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="摄像头IP" align="center" prop="cameraIp" />
      <el-table-column label="RTSP端口(默认554)" align="center" prop="cameraPort" />
      <el-table-column label="摄像头账号" align="center" prop="cameraUsername" />
      <el-table-column label="摄像头密码" align="center" prop="cameraPassword" />
      <el-table-column label="完整RTSP(优先使用)" align="center" prop="cameraRtspUrl" />
      <el-table-column label="厂商" align="center" prop="vendor" />
      <el-table-column label="摄像头型号" align="center" prop="model" />
      <el-table-column label="启动时是否自动拉流" align="center" prop="autoStart" />
      <el-table-column label="状态: 0-离线, 1-在线" align="center" prop="status" />
      <el-table-column label="${comment}" align="center" prop="httpPort" />
      <el-table-column label="公里数" align="center" prop="km" />
      <el-table-column label="上一个杆子" align="center" prop="lastPole" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['iot:poles:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['iot:poles:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改立杆节点对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="立杆编码" prop="poleCode">
          <el-input v-model="form.poleCode" placeholder="请输入立杆编码" />
        </el-form-item>
        <el-form-item label="立杆名称" prop="poleName">
          <el-input v-model="form.poleName" placeholder="请输入立杆名称" />
        </el-form-item>
        <el-form-item label="位置描述" prop="locationName">
          <el-input v-model="form.locationName" placeholder="请输入位置描述" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="摄像头IP" prop="cameraIp">
          <el-input v-model="form.cameraIp" placeholder="请输入摄像头IP" />
        </el-form-item>
        <el-form-item label="RTSP端口(默认554)" prop="cameraPort">
          <el-input v-model="form.cameraPort" placeholder="请输入RTSP端口(默认554)" />
        </el-form-item>
        <el-form-item label="摄像头账号" prop="cameraUsername">
          <el-input v-model="form.cameraUsername" placeholder="请输入摄像头账号" />
        </el-form-item>
        <el-form-item label="摄像头密码" prop="cameraPassword">
          <el-input v-model="form.cameraPassword" placeholder="请输入摄像头密码" />
        </el-form-item>
        <el-form-item label="完整RTSP(优先使用)" prop="cameraRtspUrl">
          <el-input v-model="form.cameraRtspUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="厂商" prop="vendor">
          <el-input v-model="form.vendor" placeholder="请输入厂商" />
        </el-form-item>
        <el-form-item label="摄像头型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入摄像头型号" />
        </el-form-item>
        <el-form-item label="启动时是否自动拉流" prop="autoStart">
          <el-input v-model="form.autoStart" placeholder="请输入启动时是否自动拉流" />
        </el-form-item>
        <el-form-item label="${comment}" prop="httpPort">
          <el-input v-model="form.httpPort" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="公里数" prop="km">
          <el-input v-model="form.km" placeholder="请输入公里数" />
        </el-form-item>
        <el-form-item label="上一个杆子" prop="lastPole">
          <el-input v-model="form.lastPole" placeholder="请输入上一个杆子" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPoles, getPoles, delPoles, addPoles, updatePoles } from "@/api/iot/poles";

export default {
  name: "Poles",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 立杆节点表格数据
      polesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        poleCode: null,
        poleName: null,
        locationName: null,
        longitude: null,
        latitude: null,
        cameraIp: null,
        cameraPort: null,
        cameraUsername: null,
        cameraPassword: null,
        cameraRtspUrl: null,
        vendor: null,
        model: null,
        autoStart: null,
        status: null,
        httpPort: null,
        km: null,
        lastPole: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        poleCode: [
          { required: true, message: "立杆编码不能为空", trigger: "blur" }
        ],
        poleName: [
          { required: true, message: "立杆名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询立杆节点列表 */
    getList() {
      this.loading = true;
      listPoles(this.queryParams).then(response => {
        this.polesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        poleCode: null,
        poleName: null,
        locationName: null,
        longitude: null,
        latitude: null,
        cameraIp: null,
        cameraPort: null,
        cameraUsername: null,
        cameraPassword: null,
        cameraRtspUrl: null,
        vendor: null,
        model: null,
        autoStart: null,
        status: 0,
        createTime: null,
        httpPort: null,
        km: null,
        lastPole: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加立杆节点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPoles(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改立杆节点";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePoles(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPoles(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除立杆节点编号为"' + ids + '"的数据项？').then(function() {
        return delPoles(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('iot/poles/export', {
        ...this.queryParams
      }, `poles_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
